package com.hk.employeemanagementsystem.web.controller;

import com.hk.employeemanagementsystem.dto.UpdateUserRoleDto;
import com.hk.employeemanagementsystem.entity.UserEntity;
import com.hk.employeemanagementsystem.service.UserEntityService;
import com.hk.employeemanagementsystem.util.web.WebCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by 何坤
 * @Classname UserController
 * @Description TODO
 * @Date 2020/2/13 13:41
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserEntityService userEntityServiceImpl;

    @GetMapping(value = "/get/{keyword}")
    public Map<String, Object> selectUserByKeyword(@PathVariable(required = false) String keyword){
        Map<String, Object> tips = new HashMap<>();
        try {
            List<UserEntity> userEntities = userEntityServiceImpl.selectUserByKeyword(keyword);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
            tips.put("data", userEntities);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }


    @PutMapping(value = "/put")
    public Map<String, Object> updateEnabled(HttpServletRequest request){
        Map<String, Object> tips = new HashMap<>();
        try {
            Integer userId = Integer.valueOf(request.getParameter("id"));
            Boolean enabled = Boolean.valueOf(request.getParameter("enabled"));
            userEntityServiceImpl.updateUserEnableById(userId, enabled);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_UPDATE);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_UPDATE);
        }
        return tips;
    }


    @GetMapping("/get/one/{userId}")
    public Map<String, Object> getUserById(@PathVariable("userId") Integer userId){
        Map<String, Object> tips = new HashMap<>();
        try {
            UserEntity byId = userEntityServiceImpl.getUserById(userId);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
            tips.put("data", byId);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }


    @DeleteMapping("/delete/{userId}")
    public Map<String, Object> deleteUserById(@PathVariable("userId") Integer userId){
        Map<String, Object> tips = new HashMap<>();
        try {
            userEntityServiceImpl.removeById(userId);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }

    @PutMapping("/addUserRole")
    public Map<String, Object> addUserRole(@RequestBody UpdateUserRoleDto userRoleDto){
        Map<String, Object> tips = new HashMap<>();
        try {
            String phone = userRoleDto.getPhone();
            Integer[] rids = userRoleDto.getRids();
            if (userEntityServiceImpl.addRolesForUser(phone, rids) == rids.length) {
                tips.put(WebCode.SUCCESS, true);
                tips.put(WebCode.MSG, WebCode.SUCCESS_UPDATE);
            } else {
                tips.put(WebCode.SUCCESS, false);
                tips.put(WebCode.MSG, WebCode.FAILURE_UPDATE);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_UPDATE);
        }
        return tips;
    }

}
